#ifndef __SHAREDPOINTER_H_
#define __SHAREDPOINTER_H_
#include "KLib.h"

SK

/*
TODO: Complete and test this
*/

template <class T>
class SharedPointer
{
private:
    T *obj;
    u32 *refCount;
public:
    SharedPointer<T> ()
    {
        obj = NULL;
        refCount = NULL;
    }

    SharedPointer<T> (T *object)
    {
        obj = object;
        refCount = new u32;
        *refCount = 1;
    }
    SharedPointer<T> (const SharedPointer& other)
    {
        refCount = other.refCount;
        (*refCount)++;
        obj = other.obj;
    }

    SharedPointer<T>& operator =(const SharedPointer<T>& other)
    {
        if (refCount)
            (*refCount)--;

        refCount = other.refCount;
        (*refCount)++;
        obj = other.obj;
        return *this;
    }

    ~SharedPointer ()
    {
        if (*refCount == 1)
        {
            delete obj;
            delete refCount;
        }
        else
        {
            (*refCount)--;
        }
    }

    T* SharedPointer<T>::operator ->()
    {
        return obj;
    }

    T* SharedPointer<T>::operator &()
    {
        return &obj;
    }
};

EK

#endif